﻿using AutoMapper;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Models.Manage.ProjectGroup
{
    /// <summary>
    /// 工作组视图对象
    /// </summary>
    public class CorpGroupDto
    {
        public IEnumerable<ProjectGroupDto> ProjectGroupDto { get; set; }

        public IEnumerable<VueTree<int>> CorpGroupTree { get; set; }
    }

    public static partial class CorpGroupExceptions
    {
        public static CorpGroupDto AsCorpGroupDto(this IEnumerable<Modules.Project.Entitys.ProjectGroup> projectGroups, ProjectInfo projectInfo, IApplicationContext applicationContext)
        {
            CorpGroupDto dto = new CorpGroupDto();
            List<ProjectGroupDto> projectGroupDtos = new List<ProjectGroupDto>();
            var mapper = applicationContext.GetService<IMapper>();

            var corpInfos = applicationContext.GetService<CorpService>().GetCorpsByProjectId(projectInfo.ProjectId);
            var projectGroupMembers = applicationContext.GetService<ProjectGroupMemberService>().GetProjectGroupMembersByProjectId(projectInfo.ProjectId);

            var projectCorpService = applicationContext.GetService<ProjectCorpService>();
            var projectCorps = projectCorpService.GetProjectCorpsByProjectId(projectInfo.ProjectId);
            var persons = applicationContext.GetService<PersonService>().GetPersonsByPersonIds(projectGroupMembers.Select(a => a.PersonId));
            foreach (var item in projectGroups)
            {
                var corp = corpInfos.FirstOrDefault(a => a.CorpId == item.CorpId);
                if (corp == null) continue;
                var person = persons.FirstOrDefault(i => i.PersonId == (projectGroupMembers.FirstOrDefault(a => a.GroupId == item.GroupId && a.IsLeader == true)?.PersonId ?? 0));
                ProjectGroupDto projectGroupDto = new ProjectGroupDto();
                mapper.Map(item, projectGroupDto);
                projectGroupDto.ProjectName = projectInfo.Name;
                projectGroupDto.CorpId = item.CorpId;
                projectGroupDto.CorpName = corpInfos.FirstOrDefault(a => a.CorpId == item.CorpId)?.Name;
                projectGroupDto.PersonCount = projectGroupMembers.Where(a => a.GroupId == projectGroupDto.GroupId && a.IsManager == false).Count();
                projectGroupDto.GroupMemberLeader = person?.Name;
                projectGroupDto.GroupMemberLeaderMobile = person?.Mobile;
                projectGroupDtos.Add(projectGroupDto);
            }
            dto.ProjectGroupDto = projectGroupDtos;

            List<VueTree<int>> CorpGroupTree = new();
            foreach (var corp in corpInfos)
            {
                var newProjectGroups = projectGroups.Where(a => a.CorpId == corp.CorpId);
                var projectCorp = projectCorps.FirstOrDefault(a => a.CorpId == corp.CorpId);

                string roleName = projectCorp?.Role.GetDescription() ?? string.Empty;
                if (projectInfo.LumpContractorCorpId == corp.CorpId)
                    roleName = "总承包";

                VueTree<int> corpTree = new()
                {
                    Label = corp.Name ?? string.Empty,
                    Id = corp.CorpId,
                    Extend1 = "Corp",
                    Extend2 = roleName,
                    Icon = "address-card",
                    Value = corp.CorpId,
                    Children = newProjectGroups.AsVueTree(),
                    leaf = !newProjectGroups.Any()
                };

                if (!CorpGroupTree.Any(i => i.Value == corp.CorpId))
                {
                    CorpGroupTree.Add(corpTree);
                }
            }
            dto.CorpGroupTree = CorpGroupTree;
            return dto;
        }
    }
}